﻿using App.Dal;
using App.Model;
using App.Untiys;
using Sunny.UI;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;

namespace App.Desk
{
    public partial class FCustomMain : UIForm
    {
        private static List<SysMenu> menus;
        public FCustomMain()
        {
            InitializeComponent();
            Text = Config.SampingerName;

            MainTabControl = mainTabControl;

            //设置FTitlePage1为主页面，不能被关闭
            var mainPage = new Pro.Sampling.FrmTrain();
            mainTabControl.MainPage = mainPage.Text = "采样中控台";
            AddPage(mainPage);

        }

        /// <summary>
        /// 初始化菜单
        /// </summary>
        public void BindMunus()
        {
            menus = ConfigServices.GetNoDisableNoAdminMenus(Config.CurrentUser.Authoritys);
            if (!menus.IsNotNull()) return;
            var parentMenu = menus.Where(m => m.Parent == 0).ToList();
            TreeNode parentNode;
            parentMenu.ForEach(item =>
            {
                parentNode = Aside.CreateNode(item.Name, item.IconIndex, 24, item.Id);
                var childs = menus.Where(m => m.Parent == item.Id).ToList();
                childs.ForEach(child =>
                {
                    Aside.CreateChildNode(parentNode, child.Name, child.IconIndex, 24, child.Id);
                });
            });
        }

        private void Aside_MenuItemClick(TreeNode node, NavMenuItem item, int pageIndex)
        {
            var assembly = Assembly.GetExecutingAssembly();
            if (!ExistPage(pageIndex))
            {
                var menu = menus.Where(m => m.Id == pageIndex).First();
                if (menu == null) return;
                if (menu.Parent == 0) return;
                if (string.IsNullOrEmpty(menu.AssemblyPath)) return;
                UIPage childPage = assembly.CreateInstance(menu.AssemblyPath) as UIPage;
                if (childPage != null)
                {
                    childPage.Text = menu.Name;
                    childPage.Symbol = menu.IconIndex;
                    AddPage(childPage, pageIndex);
                }
            }
            SelectPage(pageIndex);
        }

        private void FCustomMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void FCustomMain_Load(object sender, System.EventArgs e)
        {
            BindMunus();
        }

        private void FCustomMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (UIMessageBox.ShowAsk("是否确定退出系统?"))
            {
                //Application.Exit();
            }
            else
            {
                e.Cancel = true;
            }
        }
    }
}
